home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / misc / Fudgit233.lha / Source / src / alloca.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-14  |  5.4 KB  |  196 lines

  1. /*
  2.     alloca -- (mostly) portable public-domain implementation -- D A Gwyn
  3.  
  4.     last edit:    86/05/30    rms
  5.        Use xmalloc instead of malloc.
  6.  
  7.     This implementation of the PWB library alloca() function,
  8.     which is used to allocate space off the run-time stack so
  9.     that it is automatically reclaimed upon procedure exit,
  10.     was inspired by discussions with J. Q. Johnson of Cornell.
  11.  
  12.     It should work under any C implementation that uses an
  13.     actual procedure stack (as opposed to a linked list of
  14.     frames).  There are some preprocessor constants that can
  15.     be defined when compiling for your specific system, for
  16.     improved efficiency; however, the defaults should be okay.
  17.  
  18.     The general concept of this implementation is to keep
  19.     track of all alloca()-allocated blocks, and reclaim any
  20.     that are found to be deeper in the stack than the current
  21.     invocation.  This heuristic does not reclaim storage as
  22.     soon as it becomes invalid, but it will do so eventually.
  23.  
  24.     As a special case, alloca(0) reclaims storage without
  25.     allocating any.  It is a good idea to use alloca(0) in
  26.     your main control loop, etc. to force garbage collection.
  27. */
  28. #ifndef lint
  29. static char    SCCSid[] = "@(#)alloca.c    1.1";    /* for the "what" utility */
  30. #endif
  31.  
  32. #ifdef __STDC__
  33. typedef void    *pointer;        /* generic pointer type */
  34. #else
  35. typedef char    *pointer;        /* generic pointer type */
  36. #endif
  37.  
  38. #define    NULL    0            /* null pointer constant */
  39.  
  40. #include <stdlib.h>
  41. static pointer xmalloc(unsigned int);
  42.  
  43. /*
  44.     Define STACK_DIRECTION if you know the direction of stack
  45.     growth for your system; otherwise it will be automatically
  46.     deduced at run-time.
  47.  
  48.     STACK_DIRECTION > 0 => grows toward higher addresses
  49.     STACK_DIRECTION < 0 => grows toward lower addresses
  50.     STACK_DIRECTION = 0 => direction of growth unknown
  51. */
  52.  
  53. #ifndef STACK_DIRECTION
  54. #define    STACK_DIRECTION    0        /* direction unknown */
  55. #endif
  56.  
  57. #if STACK_DIRECTION != 0
  58.  
  59. #define    STACK_DIR    STACK_DIRECTION    /* known at compile-time */
  60.  
  61. #else    /* STACK_DIRECTION == 0; need run-time code */
  62.  
  63. static int    stack_dir;        /* 1 or -1 once known */
  64. #define    STACK_DIR    stack_dir
  65.  
  66.  
  67. static void
  68. find_stack_direction (void)
  69. {
  70.   static char    *addr = NULL;    /* address of first
  71.                    `dummy', once known */
  72.   auto char    dummy;        /* to get stack address */
  73.  
  74.   if (addr == NULL)
  75.     {                /* initial entry */
  76.       addr = &dummy;
  77.  
  78.       find_stack_direction ();    /* recurse once */
  79.     }
  80.   else                /* second entry */
  81.     if (&dummy > addr)
  82.       stack_dir = 1;        /* stack grew upward */
  83.     else
  84.       stack_dir = -1;        /* stack grew downward */
  85. }
  86.  
  87. #endif    /* STACK_DIRECTION == 0 */
  88.  
  89. /*
  90.     An "alloca header" is used to:
  91.     (a) chain together all alloca()ed blocks;
  92.     (b) keep track of stack depth.
  93.  
  94.     It is very important that sizeof(header) agree with malloc()
  95.     alignment chunk size.  The following default should work okay.
  96. */
  97.  
  98. #ifndef    ALIGN_SIZE
  99. #define    ALIGN_SIZE    sizeof(double)
  100. #endif
  101.  
  102. typedef union hdr
  103. {
  104.   char    align[ALIGN_SIZE];    /* to force sizeof(header) */
  105.   struct
  106.     {
  107.       union hdr *next;        /* for chaining headers */
  108.       char *deep;        /* for stack depth measure */
  109.     } h;
  110. } header;
  111.  
  112. /*
  113.     alloca( size ) returns a pointer to at least `size' bytes of
  114.     storage which will be automatically reclaimed upon exit from
  115.     the procedure that called alloca().  Originally, this space
  116.     was supposed to be taken from the current stack frame of the
  117.     caller, but that method cannot be made to work for some
  118.     implementations of C, for example under Gould's UTX/32.
  119. */
  120.  
  121. static header *last_alloca_header = NULL; /* -> last alloca header */
  122.  
  123. pointer
  124. alloca (unsigned int size)            /* returns pointer to storage */
  125.                               /* # bytes to allocate */
  126. {
  127.   auto char    probe;        /* probes stack depth: */
  128.   register char    *depth = &probe;
  129.  
  130. #if STACK_DIRECTION == 0
  131.   if (STACK_DIR == 0)        /* unknown growth direction */
  132.     find_stack_direction ();
  133. #endif
  134.  
  135.                 /* Reclaim garbage, defined as all alloca()ed storage that
  136.                    was allocated from deeper in the stack than currently. */
  137.  
  138.   {
  139.     register header    *hp;    /* traverses linked list */
  140.  
  141.     for (hp = last_alloca_header; hp != NULL;)
  142.       if (STACK_DIR > 0 && hp->h.deep > depth
  143.       || STACK_DIR < 0 && hp->h.deep < depth)
  144.     {
  145.       register header    *np = hp->h.next;
  146.  
  147.       free ((pointer) hp);    /* collect garbage */
  148.  
  149.       hp = np;        /* -> next header */
  150.     }
  151.       else
  152.     break;            /* rest are not deeper */
  153.  
  154.     last_alloca_header = hp;    /* -> last valid storage */
  155.   }
  156.  
  157.   if (size == 0)
  158.     return NULL;        /* no allocation required */
  159.  
  160.   /* Allocate combined header + user data storage. */
  161.  
  162.   {
  163.     register pointer new = (pointer) xmalloc (sizeof (header) + size);
  164.     /* address of header */
  165.  
  166.     ((header *)new)->h.next = last_alloca_header;
  167.     ((header *)new)->h.deep = depth;
  168.  
  169.     last_alloca_header = (header *)new;
  170.  
  171.     /* User storage begins just after header. */
  172.  
  173.     return (pointer)((char *)new + sizeof(header));
  174.   }
  175. }
  176. #ifndef ERRR
  177. #define ERRR (-1)
  178. #endif
  179.  
  180. #ifndef __STDIO__
  181. #include <stdio.h>
  182. #endif
  183.  
  184. static pointer xmalloc(unsigned int size)
  185. {
  186.     register long *val;
  187.  
  188.     val = (long *) malloc (size);
  189.  
  190.     if (!val && size) {
  191.         fputs("alloca: Allocation problem.\n", stderr);
  192.         Ft_catcher(ERRR);
  193.     }
  194.     return((pointer) val);
  195. }
  196.